package com.ruoyi.ldzlsb.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.ldzlsb.mapper.SbEquipmentStatusLogMapper;
import com.ruoyi.ldzlsb.service.ISbEquipmentStatusLogService;
import com.ruoyi.ldzlsb.domain.SbEquipmentStatusLog;

/**
 * 设备状态日志Service业务层处理
 */
@Service
public class SbEquipmentStatusLogServiceImpl implements ISbEquipmentStatusLogService 
{
    @Autowired
    private SbEquipmentStatusLogMapper sbEquipmentStatusLogMapper;

    /**
     * 查询设备状态日志
     * 
     * @param logId 设备状态日志主键
     * @return 设备状态日志
     */
    @Override
    public SbEquipmentStatusLog selectSbEquipmentStatusLogById(Long logId)
    {
        return sbEquipmentStatusLogMapper.selectSbEquipmentStatusLogByLogId(logId);
    }

    /**
     * 查询设备状态日志列表
     * 
     * @param sbEquipmentStatusLog 设备状态日志
     * @return 设备状态日志
     */
    @Override
    public List<SbEquipmentStatusLog> selectSbEquipmentStatusLogList(SbEquipmentStatusLog sbEquipmentStatusLog)
    {
        return sbEquipmentStatusLogMapper.selectSbEquipmentStatusLogList(sbEquipmentStatusLog);
    }

    /**
     * 查询设备状态变更历史
     * 
     * @param equipmentId 设备ID
     * @return 状态日志集合
     */
    @Override
    public List<SbEquipmentStatusLog> selectSbEquipmentStatusLogsByEquipmentId(Long equipmentId)
    {
        return sbEquipmentStatusLogMapper.selectSbEquipmentStatusLogsByEquipmentId(equipmentId);
    }

    /**
     * 新增设备状态日志
     * 
     * @param sbEquipmentStatusLog 设备状态日志
     * @return 结果
     */
    @Override
    public int insertSbEquipmentStatusLog(SbEquipmentStatusLog sbEquipmentStatusLog)
    {
        sbEquipmentStatusLog.setCreateTime(DateUtils.getNowDate());
        return sbEquipmentStatusLogMapper.insertSbEquipmentStatusLog(sbEquipmentStatusLog);
    }

    /**
     * 修改设备状态日志
     * 
     * @param sbEquipmentStatusLog 设备状态日志
     * @return 结果
     */
    @Override
    public int updateSbEquipmentStatusLog(SbEquipmentStatusLog sbEquipmentStatusLog)
    {
        return sbEquipmentStatusLogMapper.updateSbEquipmentStatusLog(sbEquipmentStatusLog);
    }

    /**
     * 批量删除设备状态日志
     * 
     * @param logIds 需要删除的设备状态日志ID
     * @return 结果
     */
    @Override
    public int deleteSbEquipmentStatusLogByIds(Long[] logIds)
    {
        return sbEquipmentStatusLogMapper.deleteSbEquipmentStatusLogByLogIds(logIds);
    }

    /**
     * 删除设备状态日志信息
     * 
     * @param logId 设备状态日志ID
     * @return 结果
     */
    @Override
    public int deleteSbEquipmentStatusLogById(Long logId)
    {
        return sbEquipmentStatusLogMapper.deleteSbEquipmentStatusLogByLogId(logId);
    }
    
    /**
     * 获取设备状态统计数据
     * 
     * @return 设备状态统计数据
     */
    @Override
    public Map<String, Object> getEquipmentStatusStatistics() {
        // 模拟数据，实际应从数据库查询
        Map<String, Object> stats = new HashMap<>();
        
        // 设备总数和各状态数量
        stats.put("totalCount", 58);
        stats.put("idleCount", 23);
        stats.put("inUseCount", 25);
        stats.put("repairingCount", 8);
        stats.put("scrapCount", 2);
        
        // 各车间设备状态统计
        List<Map<String, Object>> workshopStats = new ArrayList<>();
        
        Map<String, Object> workshop1 = new HashMap<>();
        workshop1.put("workshop", "A车间");
        workshop1.put("idle_count", 8);
        workshop1.put("inuse_count", 10);
        workshop1.put("repair_count", 2);
        workshop1.put("scrap_count", 0);
        workshopStats.add(workshop1);
        
        Map<String, Object> workshop2 = new HashMap<>();
        workshop2.put("workshop", "B车间");
        workshop2.put("idle_count", 6);
        workshop2.put("inuse_count", 8);
        workshop2.put("repair_count", 3);
        workshop2.put("scrap_count", 1);
        workshopStats.add(workshop2);
        
        Map<String, Object> workshop3 = new HashMap<>();
        workshop3.put("workshop", "C车间");
        workshop3.put("idle_count", 9);
        workshop3.put("inuse_count", 7);
        workshop3.put("repair_count", 3);
        workshop3.put("scrap_count", 1);
        workshopStats.add(workshop3);
        
        stats.put("workshopStats", workshopStats);
        
        return stats;
    }
    
    /**
     * 获取每月设备状态变更统计
     * 
     * @param year 年份
     * @return 每月状态变更统计数据
     */
    @Override
    public Map<String, Object> getEquipmentStatusChangeByMonth(Integer year) {
        // 模拟数据，实际应从数据库查询
        Map<String, Object> result = new HashMap<>();
        
        List<String> months = new ArrayList<>();
        List<Integer> changes = new ArrayList<>();
        
        // 生成月份列表和随机变更数量
        for (int i = 1; i <= 12; i++) {
            months.add(i + "月");
            // 模拟数据
            int change = 5 + (int)(Math.random() * 15);
            changes.add(change);
        }
        
        result.put("months", months);
        result.put("changes", changes);
        
        return result;
    }
} 